
var APP=null;
(function() {
	var Designer = function(){};

	var FILE_BAR = [
		{name: 'new', img: '../images/file_new.png', title: 'New'},
		{name: 'open', img: '../images/file_open.png', title: 'Open'},
		{name: 'save', img: '../images/file_save.png', title: 'Save'},
		{name: null, img: null, title: null}
	];

	var EDIT_BAR = [
		{name: 'cut', img: '../images/edit_cut.png', title: 'New'},
		{name: 'copy', img: '../images/edit_copy.png', title: 'Open'},
		{name: 'paste', img: '../images/edit_paste.png', title: 'Save'},
		{name: null, img: null, title: null}
	];

	var VIEW_BAR = [
		{name: 'zoomin', img: '../images/zoomin.png', title: 'New'},
		{name: 'zoomout', img: '../images/zoomout.png', title: 'Open'},
		{name: null, img: null, title: null}
	];


	Designer.prototype.init_toolbar = function() {
		this.toolBar = new G.Widget.ToolBar(
			{ 'file': FILE_BAR, 'edit': EDIT_BAR, 'view': VIEW_BAR });
		this.toolBar.render('TOOL_BAR');

		this.toolBar.bind('view/zoomin', this.board.zoom_in, this.board);
		this.toolBar.bind('view/zoomout', this.board.zoom_out, this.board);

		this.toolBar.bind('file/new', this.file_new, this);
	}

	Designer.prototype.file_new = function()
	{

	}

	Designer.prototype.zoom_in = function()
	{
		if (this.board)	this.board.zoom_in();
	}

	Designer.prototype.zoom_out= function()
	{
		if (this.board)	this.board.zoom_out();
	}


	Designer.prototype.create_sb_field= function(w, locate) 
	{
		var locate = locate || 'l';
		var div = document.createElement('div');
		div.className = 'statusbar';
		div.style.min_width = w;

		// Float it to right
		if (locate == 'r') {
			div.style.float = 'right';
		}
		this.statusBar.appendChild(div);
		return div;
	}

	Designer.prototype.init_statusbar = function() 
	{
		this.statusBar = document.getElementById('bottom');
		this.sbCoordinate = this.create_sb_field(100, 'r');
		this.sbScale = this.create_sb_field(100, 'r');
		this.sbDrawMode = this.create_sb_field(200);
	}

	Designer.prototype.init_project_view = function()
	{
		this.prjView = G('PRJ_VIEW').BarView([
				{name: 'schematic', title: 'Schematic'},
				{name: 'layout', title: 'PCB'},
				{name: 'symbolLib', title: 'Symbol Lib'},
				{name: 'pcbLib', title: 'PCB Library'}
		]);


		var em = this.prjView.get_body('schematic');
		this._schTreeView = G(em).TreeView();
		this._schTreeView.bind('onselectchanged', this.on_sch_page_change, this);
	}

	Designer.prototype.on_sch_page_change = function(evt)
	{
		this.schEditor.load_sch_page(evt.data);
		this.currentEditor = this.schEditor;
		this.currentPage = evt.path;
	}


	Designer.prototype.load_sch_page = function(pgdata)
	{
		this.schEditor.load_sch_page(pgdata);
		this.schEditor.run();
	}

	Designer.prototype.init = function() 
	{
		this.board = G('center').SVGBoard(4000, 3000);

		this.init_toolbar();
		this.init_statusbar();
		this.init_project_view();

		this.schEditor = new G.APP.SchEditor(this.board);
		//this.pcbEditor = new G.APP.PcbEditor();

		this.emStatus = document.getElementById('bottom');
		this.board.bind('oncursormove', this.show_cursor_xy, this);
		this.board.bind('onscalechanged', this.show_scale, this);
		this.board.bind('ondrawmodechanged', this.show_draw_mode, this);

		this.currentEditor = null;

		this.new_project();
		this.board.enter_select_mode();
	}

	Designer.prototype.show_draw_mode = function(mode) 
	{
		this.sbDrawMode.innerHTML = mode;
	}
	Designer.prototype.show_scale = function(scale) 
	{
		this.sbScale.innerHTML = scale.scale + ' (' + scale.x + ',' + scale.y + ')';
	}

	Designer.prototype.show_cursor_xy = function(p) 
	{
		this.sbCoordinate.innerHTML = p.x + ', ' + p.y + ' -- ' 
		+ this.board._scaleMax + ',' + this.board._scaleMin;
	}

	Designer.prototype.new_project = function()
	{
		this.project = new G.APP.EDAProject();
		this._schTreeView.link_data(this.project.schTree);
		this._schTreeView.set_current('schematic/page1');
		this.load_sch_page(this._schTreeView.get_current_data());
		this.schEditor.run();
	}
	APP = new Designer();
})();

